package com.alan.controller;

import com.alan.entity.teach;
import com.alan.entity.teacherstudent;
import com.alan.repository.StudentRepository;
import com.alan.repository.TeacherRepository;
import com.alan.repository.impl.StudentRepositoryImpl;
import com.alan.repository.impl.TeacherRepositoryImpl;
import com.alan.service.AdminService;
import com.alan.service.TeacherService;
import com.alan.service.impl.AdminServiceImpl;
import com.alan.service.impl.TeacherServiceIpml;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {
    private TeacherService teacherService = new TeacherServiceIpml();
    private StudentRepository studentRepository = new StudentRepositoryImpl();
    private TeacherRepository teacherRepository = new TeacherRepositoryImpl();
    private AdminService adminService = new AdminServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        HttpSession session = req.getSession();
        teach teach = (teach) session.getAttribute("teach");
        switch (method) {
            case "find":
                String pagestr = req.getParameter("page");
                Integer page = Integer.parseInt(pagestr);
                List<teacherstudent> list = teacherService.findall(teach.getT_class(), page);
                req.setAttribute("list", list);
                req.setAttribute("dateprepage", 10);
                req.setAttribute("currentpage", page);
                req.setAttribute("sumpages", teacherService.getsumpages(teach.getT_class()));
                req.getRequestDispatcher("/teacher/teacherstudent.jsp").forward(req, resp);
                break;
            case "marks":
                pagestr = req.getParameter("page");
                Integer page1 = Integer.parseInt(pagestr);
                List<teacherstudent> list1 = teacherService.findall(teach.getT_class(), page1);
                req.setAttribute("list1", list1);
                req.setAttribute("dateprepage", 10);
                req.setAttribute("currentpage", page1);
                req.setAttribute("sumpages", teacherService.getsumpages(teach.getT_class()));
                req.getRequestDispatcher("/teacher/studentmarks.jsp").forward(req, resp);
                break;

            case "change":
                req.setAttribute("teach", teach);
                req.getRequestDispatcher("teacher/ChangePassword.jsp").forward(req, resp);
                break;
            case "teacherself":
                req.setAttribute("teach", teach);
                req.getRequestDispatcher("/teacher/teacherself.jsp").forward(req, resp);
                break;
            case "out":
                session.invalidate();
                req.setAttribute("info","退出成功！老师您辛苦了！");
                req.getRequestDispatcher("/Confirm_or_cancel/outsuccess.jsp").forward(req,resp);
                break;


        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");

        switch (method) {

            case "change":
                String name = req.getParameter("name");
                String Npassword = req.getParameter("Npassword");
                teacherRepository.change(name, Npassword);
                resp.sendRedirect("teacher/teacher.jsp");
                break;

        }

    }
}
